package cn.acewill.yxd.yunxundian.adapter;

import android.app.Application;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.HashMap;
import java.util.List;

import cn.acewill.yxd.yunxundian.R;
import cn.acewill.yxd.yunxundian.YXDAPP;
import cn.acewill.yxd.yunxundian.model.reportHeadBean;
import cn.acewill.yxd.yunxundian.model.reportTailBean;
import cn.acewill.yxd.yunxundian.model.tReportHead;
import cn.acewill.yxd.yunxundian.model.tReportTail;

/**
 * Created by XXX on 2017/9/26.
 */

public class JibenInfoRecycleApdater extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    /*
    * 思路：
    * 将两个集合传入，recycleview进行分区，getviewwithtype,
    * 监听ontextchange，将值存入map中，已每个list的下标为基准，确认获取最终值，保存到本地，第二次登录客查看本地的数据*/

    //判断list类型
    public static final int ITEM_TYPE_HEADER = 0;
    public static final int ITEM_TYPE_CONTENT = 1;
    public static final int ITEM_TYPE_TITLe =2 ;
    public static final int ITEM_TYPE_NODATA =3 ;
    protected YXDAPP yxdapp=new YXDAPP();
    protected DbManager db = x.getDb(yxdapp.getDaoConfig());
    private Context context;
    private List<reportHeadBean> headList;
    private List<reportTailBean> tailList;
    //hasmap保存编辑的数据
    private HashMap<Integer,String> mheadValue = new HashMap<>();
    private HashMap<Integer,String> mtailValue = new HashMap<>();
    private List<tReportHead> tReportHeads;
    private int schid=0;


   public JibenInfoRecycleApdater(Context context, List<reportHeadBean> headList,List<reportTailBean> tailList,int inSchid){
        this.context = context;
        this.headList = headList;
        this.tailList = tailList;
        this.schid = inSchid;
    }

    public int getHeadCount(){
        return headList!=null?headList.size():0;

    }
    public int getContentCount(){
        return tailList!=null?tailList.size():0;
    }

    //返回给页面
    public  HashMap<Integer, String> getheanList(){
        return mheadValue;
    }
    public  HashMap<Integer, String> gettailList(){
        return mtailValue;
    }



    @Override
    public int getItemViewType(int position) {

        int dataItemCount = getHeadCount() + getContentCount();
        if (position == 0) { //第一个数据
            Log.e("count_postion_title", position + "");
            return ITEM_TYPE_TITLe;
        } else if (getHeadCount()==0&&position==1){
            Log.e("count_postion_nodata1", position + "");
            return ITEM_TYPE_NODATA;
        }else if (getHeadCount()==0&&position==2){
            Log.e("count_postion_title2", position + "");
            return ITEM_TYPE_TITLe;
        }
        else if (getHeadCount()==0&&getContentCount()==0&&position==3){
            Log.e("count_postion_nodata2", position + "");
            return ITEM_TYPE_NODATA;
        }else if (getHeadCount()==0&&getContentCount()>0&&position>2){
            Log.e("count_postion_content", position + "");
            return ITEM_TYPE_CONTENT;
        }
        else if (getHeadCount()>0&&position==getHeadCount() + 1){
            Log.e("count_postion_title3", position + "");
            return ITEM_TYPE_TITLe;
        }else if (getHeadCount()>0&&position>0&&position<getHeadCount()+1){
            Log.e("count_postion_head", position + "");
                return ITEM_TYPE_HEADER;
        }else{
            Log.e("count_postion_content2", position + "");
            return ITEM_TYPE_CONTENT;
        }




//
//        else if (getContentCount() > 0 & position == getHeadCount() + 1) {//分割数据处
//            Log.e("count_postion_title2", position + "");
//            return ITEM_TYPE_TITLe;
//        } else if (getHeadCount() == 0 && getContentCount() > 0&&position==0) {//
//            return ITEM_TYPE_TITLe;
//        } else if (getContentCount() == 0) {//底部没数据
//            return -1;
//        } else if (getHeadCount() != 0 && position > 0 && position < getHeadCount() + 1) {
//            Log.e("count_postion_head", position + "");
//            return ITEM_TYPE_HEADER;
//        } else {
//            Log.e("count_postion_tail", position + "");
//            return ITEM_TYPE_CONTENT;
//        }
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView;

        if (viewType == ITEM_TYPE_TITLe){
            itemView = LayoutInflater.from(context).inflate(R.layout.showtext,parent,false);
            return new titleViewHolder(itemView);
        }
        if (viewType==ITEM_TYPE_HEADER){
            itemView = LayoutInflater.from(context).inflate(R.layout.head_tail_view,parent,false);
            return new headViewHolder(itemView);
        }if (viewType==ITEM_TYPE_CONTENT){
            itemView=LayoutInflater.from(context).inflate(R.layout.head_tail_view,parent,false);
            return new contentViewHolder(itemView);
        }if (viewType==ITEM_TYPE_NODATA){
            itemView = LayoutInflater.from(context).inflate(R.layout.normal_text_view,parent,false);
            return new nodataViewHolder(itemView);
        }

        return null;

    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof titleViewHolder){
            if (position==0){
                Log.e("count_holder1",position+"");
                ((titleViewHolder) holder).title.setText("基本信息");
            }else{
                Log.e("count_holder1",position+"");
                ((titleViewHolder) holder).title.setText("备注信息");
            }
        }
        if (holder instanceof headViewHolder){
            Log.e("count_holder2",position+"");
            final reportHeadBean headBean = headList.get(position-1); //去掉标题position
            ((headViewHolder) holder).headName.setText(headBean.getHeadName()+":");
            try {
                tReportHead tts= db.selector(tReportHead.class).where("reportHeadId","=",headList.get(position-1).getID()).and("ID","=", schid).findFirst();
                if (tts!=null){
                    ((headViewHolder) holder).headValue.setText(tts.getValueStr());
                    ((headViewHolder) holder).headValue.setSelection(((headViewHolder) holder).headValue.length());
                }
            } catch (DbException e) {
                e.printStackTrace();
            }
            ((headViewHolder) holder).headValue.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    int position =holder.getLayoutPosition()-1;
                   // int reportid=headBean.getID();
                    mheadValue.put(position,s.toString());
                    ((headViewHolder) holder).headValue.setSelection(((headViewHolder) holder).headValue.length());
                }
            });

        }
        if (holder instanceof contentViewHolder){
            Log.e("count_holder3",position+"");
            final reportTailBean tailBean;
            if (getHeadCount()==0){
                tailBean = tailList.get(position-3);
            }else{
                tailBean = tailList.get(position-2-getHeadCount());
            }
            ((contentViewHolder) holder).tailName.setText(tailBean.getTailName()+":");
            tReportTail tail = null;
            try {
                if (getHeadCount()==0){
                    tail= db.selector(tReportTail.class).where("reportTailId","=",tailList.get(position-3).getID()).and("ID","=", schid).findFirst();

                }else{
                     tail= db.selector(tReportTail.class).where("reportTailId","=",tailList.get(position-2-getHeadCount()).getID()).and("ID","=", schid).findFirst();
                }
                if (tail!=null){
                    ((contentViewHolder) holder).tailValue.setText(tail.getValueStr());
                    ((contentViewHolder) holder).tailValue.setSelection(((contentViewHolder) holder).tailValue.length());
                }
            } catch (DbException e) {
                e.printStackTrace();
            }

            ((contentViewHolder) holder).tailValue.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    int position;
                    if (getContentCount()==0){
                         position = holder.getLayoutPosition()-3;
                    }else{
                         position = holder.getLayoutPosition()-2-getHeadCount();
                    }
                   // int reportid=tailBean.getID();
                    mtailValue.put(position,s.toString());
                    ((contentViewHolder) holder).tailValue.setSelection(((contentViewHolder) holder).tailValue.length());
                }
            });


        }

    }

    @Override
    public int getItemCount() { //增加标题
        if (getHeadCount()==0&&getContentCount()==0){
            return getHeadCount()+getContentCount()+4;
        }else if (getHeadCount()==0&&getContentCount()>0){
            return getHeadCount()+getContentCount()+3;
        }else{
            return getHeadCount()+getContentCount()+2;
        }
    }
}

class titleViewHolder extends RecyclerView.ViewHolder {
    TextView title;
    public titleViewHolder(View itemView) {
        super(itemView);
        title= (TextView) itemView.findViewById(R.id.shopName);
    }
}

class headViewHolder extends RecyclerView.ViewHolder {

    TextView headName;
    EditText headValue;

    public headViewHolder(View itemView) {
        super(itemView);
        headName = (TextView) itemView.findViewById(R.id.itemName);
        headValue = (EditText) itemView.findViewById(R.id.itemValue);
    }
}
class contentViewHolder extends RecyclerView.ViewHolder {
    TextView tailName;
    EditText tailValue;
    public contentViewHolder(View itemView) {
        super(itemView);
        tailName = (TextView) itemView.findViewById(R.id.itemName);
        tailValue= (EditText) itemView.findViewById(R.id.itemValue);
    }

}
class nodataViewHolder extends RecyclerView.ViewHolder {
    public nodataViewHolder(View itemView) {
        super(itemView);
    }
}
